<template>
	<div id="save" v-if="actidx!=2" style="width: 100%; border: 1px solid blue; ">
			<h2 v-if="actidx==1"  align="center">学生添加</h2>
			<h2 v-if="actidx==0" align="center">学生修改</h2>
			<hr />
			<table style="border: 1px solid; width: 100%;">
				<tr>
					<td align="center">学生姓名</td>
					<td align="center">
						<input type="text" v-model="formObj.uname" />
					</td>
				</tr>
	
				<tr>
	
					<td align="center">手机号</td>
					<td align="center">
						<input type="text" v-model="formObj.phone"  />
	
					</td>
				</tr>
	
				<tr>
					<td align="center">课程</td>
					<td align="center">
						<select v-model="formObj.clz.clzno" style="width: 70%; font-size: 1pc;">
							<option align="center" v-for="c in clzs" v-bind:value="c.clzno">
								{{c.clzname}}
							</option>
						</select>
					</td>
				</tr>
			</table>
			<div v-if="actidx==0" style="width:20vb;height: 20vb">
				<img v-bind:src="`${this.$ip}/api/public/showimg/`+formObj.pic" style="width: 100%;height: 100%">
			</div>
			<p>图片: <input type="file" ref="img"> </p>
	
			<div style="display: flex; justify-content: space-between;">
				<p>&nbsp;</p>
				<p><button v-on:click="save()">确定</button></p>
				<p> <button v-on:click="saveout()">取消</button></p>
				<p>&nbsp;</p>
			</div>
		</div>
</template>

<script>
		export default {
			name: 'Stu',
			data() {
				return {
					formObj: {clz:{}},
					stus: [],
					clzs: [],
					actmsg: ['修改', '添加', '显示'],
					actidx: 2,
					page:{num:1,lines:6,total:0}
				}
			},
			methods: {
				saveout(){
					this.$router.push({
						path: '/adm/stu'})
				},
				save() {
					var that = this;
					
					let url = "/api/adm/stu/add"
					if (this.actidx == 0) {
						url = "/api/adm/stu/update"
					}
					console.log(url)
					console.log(that.formObj)
					var fd = new FormData;
					fd.append("uid",  parseInt(this.formObj.uid) )
					fd.append("uname", this.formObj.uname)
					fd.append("phone", this.formObj.phone)
					fd.append("pwd", this.formObj.pwd)
					fd.append("clz.clzno", this.formObj.clz.clzno)
					   // 获取图片
					var file = this.$refs.img.files[0]; //获取文件上传第一个文件内容
					fd.append("mypic", file);
					var that = this;
					this.$axios.post(this.$ip +url, fd, {
						headers: {
							'Authorization': 'Bearer ' + window.localStorage.getItem("token")
						}
					}).then(function(resp) {
						console.log(resp.data);
						
					this.$router.push({
						path: '/adm/stu'})
					
					
					})
				},},
				mounted() {
					console.log(this.$route.query.uid)
					
					if(this.$route.query.uid==0){
					 this.actidx=1
					}
					if(this.$route.query.uid>0){
					 this.actidx=0 
					 this.$axios.get(this.$ip+"/api/adm/stu/findById/"+this.$route.query.uid,
					 {
					 	headers: {
					 		'Authorization': 'Bearer ' + window.localStorage.getItem("token")
					 	}
					 }
					 )
					 .then((resp)=>{
						 
					    this.formObj=resp.data.data
						
					 })
					}
					
					this.$axios.get(this.$ip + "/api/public/getClzs").then((resp) => {
						this.clzs = resp.data.data;
						
						console.log(this.clzs)
					})
					
				}
				}
</script>

<style>
	input{
		text-align: center
	}
</style>
